﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.Data.SqlClient;

public partial class pay_order : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["pnr"] == null)
            {
                lblError.Text = "非法网页调用！";

                return;
            }
            lblOrderNo.Text = Request.QueryString["pnr"].ToString();
            RetreiveTrip();
        }
        lblError.Text = "";
        lblPayMsg.Text = "";
        
    }
    protected void RetreiveTrip()
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        string tmp_fare,tmp_tax;
        DataSet dsRet;
        StringBuilder buf = new StringBuilder();
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsRet = instOrder.GetTrip(Convert.ToInt64(lblOrderNo.Text), 2, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 1:
                    lblError.Text = "订单找不到";
                    break;
                case 2:
                    lblError.Text = "无权查看";
                    break;
                case 3:
                    lblError.Text = "无查看本Office订单权限";
                    break;
                case 4:
                    lblError.Text = "无查看本Entity订单权限";
                    break;
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";
        buf.Append("<table><tr><th>日期</th><th>航程</th><th>航班号</th><th>舱位</th><th>成人单价</th><th>儿童单价</th><th>婴儿单价</th><th>成人燃油费</th><th>儿童燃油费</th><th>婴儿燃油费</th><th>成人税</th><th>儿童税</th><th>婴儿税</th><th>其它费</th><th>支付币种</th><th>票价兑换率</th><th>小计</th></tr>");
        foreach (DataRow row2 in dsRet.Tables[0].Rows)
        {
            tmp_fare = row2["fare_currency"].ToString();
            tmp_tax = row2["fare_currency"].ToString();
            buf.Append("<tr><td>" + Convert.ToDateTime(row2["flt_date"]).ToShortDateString() + "</td><td>" + row2["ori_airport"] + "-" + row2["des_airport"] + "</td><td>"
                + row2["airline"] + row2["flt_no"] + "</td><td>" + row2["cabin"] + "/" + row2["class"]
                + "</td><td>" + tmp_fare + Convert.ToSingle(row2["final_price"]).ToString() + "</td><td>" + tmp_fare + Convert.ToSingle(row2["chd_price"]).ToString() + "</td><td>" + tmp_fare + Convert.ToSingle(row2["inf_price"]).ToString()
                + "</td><td>" + tmp_fare + Convert.ToSingle(row2["adt_fuel"]).ToString() + "</td><td>" + tmp_fare + Convert.ToSingle(row2["chd_fuel"]).ToString() + "</td><td>" + tmp_fare + Convert.ToSingle(row2["inf_fuel"]).ToString()
                + "</td><td>" + tmp_tax + Convert.ToSingle(row2["adt_taxes"]).ToString() + "</td><td>" + tmp_tax + Convert.ToSingle(row2["chd_taxes"]).ToString() + "</td><td>" + tmp_tax + Convert.ToSingle(row2["inf_taxes"]).ToString()
                + "</td><td></td><td>" + row2["pay_currency"] + "</td><td>" + row2["fare_roe"].ToString() + "</td><td>" + row2["pay_currency"] /*+ row2["sub_total"].ToString()*/ + "</td></tr>");
        }
        buf.Append("</table>");
        litTrip.Text = buf.ToString();

    }
    protected void btnCalFare_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        float total_fare;
        lblCurrency.Text = lstCurrency.SelectedValue;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");

        ret_code = instOrder.CalOrderFare(Convert.ToInt64(lblOrderNo.Text), lblCurrency.Text, Application["UID"].ToString(), sign, out total_fare, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            lblTotalFare.Text = "?";
            return;
        }
        switch (ret_code)
        {
            case 0:
                lblError.Text = "";
                lblTotalFare.Text = total_fare.ToString();
                break;
            case 1:
                lblError.Text = "订单找不到";
                lblTotalFare.Text = "?";
                break;
            case 2:
                lblError.Text = "无权查看";
                lblTotalFare.Text = "?";
                break;
            case 3:
                lblError.Text = "无查看本Office订单权限";
                lblTotalFare.Text = "?";
                break;
            case 4:
                lblError.Text = "无查看本Entity订单权限";
                lblTotalFare.Text = "?";
                break;
            case 91:
                lblError.Text = "登录错";
                lblTotalFare.Text = "?";
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                lblTotalFare.Text = "?";
                break;
        }

    }
    protected void btnPay_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        float total_paid;
        if (lblTotalFare.Text == "?")
        {
            lblError.Text = "请先计算总票款。";
            return;
        }
        try
        {
            total_paid = Convert.ToSingle(lblTotalFare.Text);
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            return;
        }
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        ret_code = instOrder.Pay(Convert.ToInt64(lblOrderNo.Text), lblCurrency.Text, Convert.ToSingle(lblTotalFare.Text), Application["UID"].ToString(), sign, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            lblTotalFare.Text = "?";
            return;
        }

        switch (ret_code)
        {
            case 0:
                lblPayMsg.Text = "支付成功！祝您旅途愉快。届时请凭身份证到机场办理乘机手续。";
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "add_account";
                SqlParameter para = cmd.Parameters.AddWithValue("@uid", Session["UID"].ToString());
                para = cmd.Parameters.AddWithValue("@order_no", lblOrderNo.Text);
                para = cmd.Parameters.AddWithValue("@points", total_paid);
                para = cmd.Parameters.AddWithValue("@remark", "");
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code != 0)
                {
                    lblError.Text = "保存订单出错!请手记录订单号！";
                    lblPayMsg.Text = ret_msg;
                    return;
                }
                //打印行程单
                btnNext.Visible = true;
                break;
            case 1:
                lblError.Text = "更新货币兑换率失败";
                lblPayMsg.Text = ret_msg;
                break;
            case 3:
                lblError.Text = "未找到计算对象";
                lblPayMsg.Text = ret_msg;
                break;
            case 91:
                lblError.Text = "登录错";
                lblPayMsg.Text = ret_msg;
                break;
            case 101:
                lblError.Text = "101:Not sell office!";
                lblPayMsg.Text = ret_msg;
                break;
            case 102:
                lblError.Text = "写入交易额失败";
                lblPayMsg.Text = ret_msg;
                break;
            case 103:
                lblError.Text = "生成客票失败";
                lblPayMsg.Text = ret_msg;
                break;
            case 104:
                lblError.Text = "写入航段价格失败";
                lblPayMsg.Text = ret_msg;
                break;
            case 105:
                lblError.Text = "105:Generate E-tkt trips failed! Check seats status and pax count!";
                lblPayMsg.Text = ret_msg;
                break;
            case 106:
                lblError.Text = "106:Updating expiry failed!";
                lblPayMsg.Text = ret_msg;
                break;
            case 107:
                lblError.Text = "107:Total fare mismatch!";
                lblPayMsg.Text = ret_msg;
                break;
            case 110:
                lblError.Text = "110:Office not found or not valid!";
                lblPayMsg.Text = ret_msg;
                break;
            case 111:
                lblError.Text = "111:Deposit exchange rate not found!";
                lblPayMsg.Text = ret_msg;
                break;
            case 112:
                lblError.Text = "112:Lack of deposit!";
                lblPayMsg.Text = ret_msg;
                break;
            case 113:
                lblError.Text = "113:Deducting deposit failed!";
                lblPayMsg.Text = ret_msg;
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                lblPayMsg.Text = ret_msg;
                break;
        }
        if (ret_code != 0)
            return;


    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("order.aspx?pnr=" + lblOrderNo.Text);
    }
}
